package com.app.bimo.commonbusiness.view.recyclerview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * 增强型RecyclerView，可以设置头底视图
 *
 * @author Jack Tony
 * @attention 必须在设置adapter前设置header或footer, clickListener
 */
public class HeadFootRecyclerView extends RecyclerView {

    private View mHeaderView = null;

    private View mFooterView = null;
    private Toolbar toolbar;

    private GridLayoutManager.SpanSizeLookup spanSizeLookup;
    public boolean isHandUp = true;

    public HeadFootRecyclerView(Context context) {
        super(context);
    }

    public HeadFootRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeadFootRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setLayoutManager(final LayoutManager layoutManager) {
        super.setLayoutManager(layoutManager);
        if (layoutManager instanceof GridLayoutManager) {
            GridLayoutManager lm = (GridLayoutManager) layoutManager;
            if (lm.getSpanSizeLookup() != null) {
                this.spanSizeLookup = lm.getSpanSizeLookup();
            }
            lm.setSpanSizeLookup(new GridSpanSizeLookup(lm.getSpanCount()));
        }
    }

    /**
     * 设置Grid/StaggeredGrid LayoutManager的布局样式
     */
    private class GridSpanSizeLookup extends GridLayoutManager.SpanSizeLookup {

        private int mSpanSize = 1;

        public GridSpanSizeLookup(int spanSize) {
            mSpanSize = spanSize;
        }

        @Override
        public int getSpanSize(int position) {
            RecycleAdapterImpl adapter = (RecycleAdapterImpl) getAdapter();
            // 如果是头或底的类型，那么就设置横跨所有列
            if (adapter.getItemViewType(position) == RecycleAdapterImpl.VIEW_TYPES.HEADER
                    || adapter.getItemViewType(position) == RecycleAdapterImpl.VIEW_TYPES.FOOTER) {
                return mSpanSize;
            }
            if (spanSizeLookup != null) {
                if (mHeaderView != null) {
                    return spanSizeLookup.getSpanSize(position - 1);
                }
                return spanSizeLookup.getSpanSize(position);
            }
            return 1;
        }
    }

    /**
     * Set the header view of the adapter.
     */
    public void addHeaderView(View headerView) {
        mHeaderView = headerView;
    }

    /**
     * @return recycle的头部视图
     */
    public View getHeaderView() {
        return mHeaderView;
    }

    public int getcomputeVerticalScrollOffset() {
        return computeVerticalScrollOffset();
    }

    /**
     * 设置底部的视图
     */
    public void addFooterView(View footerView) {
        mFooterView = footerView;
    }

    /**
     * 得到底部的视图
     */
    public View getFooterView() {
        return mFooterView;
    }

    /**
     * 需要在设置头、低、监听器之后再调用setAdapter(Adapter adapter)来设置适配器
     */
    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        if (adapter instanceof RecycleAdapterImpl<?>) {
            ((RecycleAdapterImpl) adapter).mOnItemClickListener = mOnItemClickListener;
            ((RecycleAdapterImpl) adapter).mOnItemLongClickListener = mOnItemLongClickListener;
            ((RecycleAdapterImpl) adapter).customHeaderView = mHeaderView;
            ((RecycleAdapterImpl) adapter).customFooterView = mFooterView;
        }
    }

    /**
     * 平滑滚动到某个位置
     * <p>
     * position是否是绝对的，如果是绝对的，那么header的位置就是0
     * 如果是相对的，那么position就是相对内容的list的位置
     */
    public void smoothScrollToPosition(int position) {
        if (mHeaderView != null) {
            position++;
        }
        super.smoothScrollToPosition(position);
    }

    public void smootmScrollToTop() {
        super.smoothScrollToPosition(0);

    }

    public void scrollToTop() {
        scrollToPosition(0);

    }


    /**
     * 设置item的点击事件
     */
    private static AdapterView.OnItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }


    /**
     * 设置item的长按事件
     */
    public static AdapterView.OnItemLongClickListener mOnItemLongClickListener = null;


    public void setOnItemLongClickListener(
            AdapterView.OnItemLongClickListener listener) {
        mOnItemLongClickListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {


        if (e.getAction() == MotionEvent.ACTION_UP){
            isHandUp = true;

        }else {
            isHandUp = false;
        }

        return super.onTouchEvent(e);
    }




}
